package com.acwing.partition10;

import java.io.*;
import java.util.LinkedList;
import java.util.Queue;

/**
 * @author `RKC`
 * @date 2022/1/20 15:00
 */
public class AC904虫洞 {

    private static final int N = 510, M = 5210;
    private static int[] head = new int[N], element = new int[M], next = new int[M], weight = new int[M];
    private static int[] dist = new int[N], cnt = new int[N];
    private static boolean[] inQueue = new boolean[N];

    private static int n = 0, m1 = 0, m2 = 0, idx = 0;

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        int k = Integer.parseInt(reader.readLine());
        while (k-- > 0) {
            String[] s = reader.readLine().split("\\s+");
            n = Integer.parseInt(s[0]);
            m1 = Integer.parseInt(s[1]);
            m2 = Integer.parseInt(s[2]);
            idx = 0;
            for (int i = 1; i <= n; i++) head[i] = -1;
            for (int i = 0; i < m1; i++) {
                s = reader.readLine().split("\\s+");
                int u = Integer.parseInt(s[0]), v = Integer.parseInt(s[1]), w = Integer.parseInt(s[2]);
                createEdge(u, v, w);
                createEdge(v, u, w);
            }
            for (int i = 0; i < m2; i++) {
                s = reader.readLine().split("\\s+");
                int u = Integer.parseInt(s[0]), v = Integer.parseInt(s[1]), w = Integer.parseInt(s[2]);
                createEdge(u, v, -w);
            }
            if (spfa()) writer.write("YES\n");
            else writer.write("NO\n");
        }
        writer.flush();
    }

    private static boolean spfa() {
        Queue<Integer> queue = new LinkedList<>();
        for (int i = 1; i <= n; i++) {
            queue.add(i);
            dist[i] = cnt[i] = 0;
            inQueue[i] = true;
        }
        while (!queue.isEmpty()) {
            int u = queue.poll();
            inQueue[u] = false;
            for (int i = head[u]; i != -1; i = next[i]) {
                int v = element[i];
                if (dist[v] > dist[u] + weight[i]) {
                    dist[v] = dist[u] + weight[i];
                    cnt[v] = cnt[u] + 1;
                    if (cnt[v] >= n) return true;
                    if (inQueue[v]) continue;
                    queue.add(v);
                    inQueue[v] = true;
                }
            }
        }
        return false;
    }

    private static void createEdge(int u, int v, int w) {
        element[idx] = v;
        next[idx] = head[u];
        head[u] = idx;
        weight[idx++] = w;
    }
}
